﻿/*
* ==============================================================================
*
* File name: Operation
* Description: 
*
* Version: 1.0
* Created: 2015-07-07 10:59:44
*
* Author: Your name
* Company: Your company name
*
* ==============================================================================
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OperationLibrary
{
    /// <summary>
    /// 运算类（父类）
    /// </summary>
    public class Operation
    {
        double numberA;

        public double NumberA
        {
            get { return numberA; }
            set { numberA = value; }
        }

        double numberB;

        public double NumberB
        {
            get { return numberB; }
            set { numberB = value; }
        }

        double result;
        /// <summary>
        /// 运算结果
        /// </summary>
        public double Result
        {
            get { return result; }
            set { result = value; }
        }

        /// <summary>
        /// 虚函数，实现多态。子类中重写该函数
        /// </summary>
        /// <returns></returns>
        public virtual double GetResult()
        {
            return Result;
        }

        /// <summary>
        /// 检查输入的字符串是否准确
        /// </summary>
        /// <param name="currentNumber"></param>
        /// <param name="inputString"></param>
        /// <returns></returns>
        public static string checkNumberInput(string currentNumber, string inputString)
        {
            string result = "";

            if (inputString == ".")
            {
                if (currentNumber.Length == 0)
                    result = "0" + inputString;
                else
                    result = currentNumber + inputString;
            }
            else if (currentNumber == "0")
            {

                result = inputString;
            }
            else
            {
                result = currentNumber + inputString;
            }

            return result;
        }

    }

    #region 子类（可单独新建文件）


    /// <summary>
    /// 加法类 （要增加其他算法时，不用修改这里，只需在后面加就行（开闭原则））
    /// </summary>
    class OperationAdd : Operation
    {
        public override double GetResult()
        {
            Result = NumberA + NumberB;
            return Result;
        }
    }
    class OperationSub : Operation
    {
        public override double GetResult()
        {
            Result = NumberA - NumberB;
            return Result;
        }
    }
    class OpertionMul : Operation
    {
        public override double GetResult()
        {
            Result = NumberA * NumberB;
            return Result;
        }
    }
    class OperationDiv : Operation
    {
        public override double GetResult()
        {
            if (NumberB == 0)
                throw new Exception("除数不能为0。");
            Result = NumberA / NumberB;
            return Result;
        }
    }

    /// <summary>
    /// 平方类
    /// </summary>
    class OperationSqr : Operation
    {
        public override double GetResult()
        {
            Result = NumberB * NumberB;
            return Result;
        }
    }

    /// <summary>
    /// 平方根类
    /// </summary>
    class OperationSqrt : Operation
    {
        public override double GetResult()
        {
            if (NumberB < 0)
                throw new Exception("负数不能开平方根。");
            Result = Math.Sqrt(NumberB);
            return Result;
        }
    }

    /// <summary>
    /// 相反数类
    /// </summary>
    class OperationReverse : Operation
    {
        public override double GetResult()
        {
            Result = -NumberB;
            return Result;
        }
    }

    #endregion
}

